/**
 * @class   wosh::gui::FormObjectFactory
 * @brief   Graphic interface of wosh::ObjectFactory core-module.
 *
 * \image html extras/screenshots/WOSH.GUI.FormObjectFactory.1.png "FormObjectFactory::Tab 1"
 * \image html extras/screenshots/WOSH.GUI.FormObjectFactory.2a.png "FormObjectFactory::Tab 2a"
 * \image html extras/screenshots/WOSH.GUI.FormObjectFactory.2b.png "FormObjectFactory::Tab 2b"
 *
 * @ingroup Gui
 ****************************************************************************
 * @version 0.8.499 $Id: FormObjectFactory.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_FormObjectFactory_H__
 #define __WOSH_Gui_FormObjectFactory_H__

 #include <framework/gui/IWidget.h>
 #include <framework/gui/WoshTreeModel.h>
 #include <core/ObjectAllocator.h>
 #include <QtGui/QMenu>
 #include <framework/gui/GuiUtilities.h>


namespace Ui {
 class FormObjectFactory;
};
namespace wosh {
 namespace gui {


class WoshObjectInfoTreeModel :  public WoshTreeModel
 {
	Q_OBJECT

	public:
		WoshObjectInfoTreeModel( Tree* tree_data, QObject *parent = 0)
			: WoshTreeModel(tree_data, parent) { }
		virtual ~WoshObjectInfoTreeModel() { }

	public:
		int columnCount( const QModelIndex &parent = QModelIndex() ) const {
			(void)parent; return 4;
		 }

		QVariant data( const QModelIndex &index, int role ) const {
			if ( !index.isValid() )	return QVariant();
			TreeNode* item = static_cast<TreeNode*>(index.internalPointer());
			if ( item == NULL || item->getContent() == NULL ) return QVariant();
			const ObjectTypeInfo* objinfo = item->getContent()->as<ObjectTypeInfo>();
			if ( objinfo == NULL ) return QVariant("NULL");
			if ( role == Qt::DisplayRole || role == Qt::EditRole ) { // cos completer!
				switch (index.column()) {
					case 0: return QVariant(objinfo->name.c_str());
					case 1: return QVariant(objinfo->version);
					case 2: return QVariant(objinfo->allocable);
					case 3: return QVariant(objinfo->parent.c_str());
					default:
						return QVariant( item->getContent()->toString().c_str() );
				 }
			 }
			else if ( role == Qt::ToolTipRole ) {
				return QVariant( objinfo->name.c_str() );
			 }
			else if ( index.column() == 0 && role == Qt::DecorationRole ) {
				return QVariant( GuiUtilities::createIconFor(objinfo->name) );
			 }
			return QVariant();
		 }

		QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const {
			if (role != Qt::DisplayRole) return QVariant();
			if (orientation == Qt::Horizontal) {
				 switch(section) {
					case 0:		return QString("ClassName");
					case 1:		return QString("Version");
					case 2:		return QString("Allocable");
					case 3:		return QString("Parent");
					default:	return QVariant();
				  }
			 }
			return QVariant();
		 }

	public: // search
		QModelIndex indexOf( const std::string& classname, int column = 0 ) {
			TreeNode* rootNode = getRootNode();
			if ( rootNode == NULL ) return QModelIndex();
			int iRow = 0;
			TreeNodeT<ISerializable*>::ChildConstIterator it;
			const TreeNodeT<ISerializable*>::ChildConstIterator it_end = rootNode->getChildren().end();
			for( it=rootNode->getChildren().begin(); it!=it_end; ++it, ++iRow ) {
				if ( *it == NULL || (*it)->getContent() == NULL ) continue;
				const ObjectTypeInfo* objinfo = (*it)->getContent()->as<ObjectTypeInfo>();
				if ( objinfo == NULL ) continue;
				if ( objinfo->name != classname ) continue;
				return createIndex(iRow, column, *it);
			 }
			return QModelIndex();
		 }


}; // class def



class FormObjectFactory :  public IWidgetGeneric
 {
	Q_OBJECT

	WOSH_CLASSNAME(wosh::gui::FormObjectFactory)
	WOSH_CLASS_ABSTRACT(false)


	public:
		FormObjectFactory(QWidget *parent = 0);
		virtual ~FormObjectFactory();

	public:
		void busMessage( const Message& message, const Bus* source );

	public slots:
		void refreshAll();

	protected slots:
		void refreshRTTI();

		void btnAddAllocator_clicked();
		void btnRemoveAllocator_clicked();
		void btnAllocationQuery_clicked();
		void btnAllocatorFind_clicked();
		void treeViewObjectTypes_ContextMenu( const QPoint& pos );

	protected:
		void changeEvent(QEvent *e);
		void doCheckAllocable( const std::string& classname );
		void doAllocatorFind( const std::string& classname );

	protected:
		WoshObjectInfoTreeModel* treeModel;

		void buildMenus();
		QMenu* menuObjectType;
		QAction* menuTitle;
		QAction* menuCheckAllocable;
		QAction* menuFindAllocator;
		QAction* menuGotoParent;
		QAction* menuGotoRootNode;

	private:
		Ui::FormObjectFactory *m_ui;


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_FormObjectFactory_H__
